package com.company.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.educenter.entity.UcenterMember;
import com.company.educenter.mapper.UcenterMemberMapper;
import com.company.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.educenter.vo.RegistVo;
import com.company.servicebase.exception.MyException;
import com.company.utils.JwtUtils;
import com.company.utils.MD5;
import com.company.utils.RedisUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-04-21
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Resource
    private RedisUtil redisUtil;

    @Override
    public String login(UcenterMember user) {
        //TODO 用户只按照了手机号登录
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", user.getMobile());
        UcenterMember member = this.getOne(wrapper);

        if (member == null) {
            throw new MyException(20001, "登录失败，请检查用户名和密码！");
        }

        if (!MD5.encrypt(user.getPassword()).equals(member.getPassword())) {
            throw new MyException(20001, "密码错误，请重试！");
        }

        String jwtToken = JwtUtils.getJwtToken(member.getId(), member.getNickname());
        return jwtToken;
    }

    @Override
    public void register(RegistVo registVo) {
        String nickname = registVo.getNickname();//用户名
        String mobile = registVo.getMobile();//手机号
        String password = registVo.getPassword();//密码
        String code = registVo.getCode();//验证码

        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("nickname", nickname);
        Integer count = this.baseMapper.selectCount(new QueryWrapper<UcenterMember>().eq("nickname", nickname));
        if (count != 0) {
            throw new MyException(20001, "用户名已存在，请重试！");
        }
        count = this.baseMapper.selectCount(new QueryWrapper<UcenterMember>().eq("mobile", mobile));
        if (count != 0) {
            throw new MyException(20001, "手机已被注册，请重试！");
        }

        String redisCode = (String)redisUtil.get(mobile);
        if (!code.equals(redisCode)) {
            throw new MyException(20001, "验证码错误，请重试！");
        }

        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        this.save(member);

    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        UcenterMember member = this.getOne(new QueryWrapper<UcenterMember>().eq("openid", openid));
        return member;
    }

    @Override
    public Integer getRegisterCount(String day) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.apply("date_format(gmt_create,'%Y-%m-%d') = {0}", day);
        Integer count = this.baseMapper.selectCount(wrapper);
        return count;
    }
}
